<?php

class Admin_PostController extends Zend_Controller_Action
{

    public function init()
    {
		$this->mergeQueryString();
        $this->_auth = Zend_Auth::getInstance();
    	
        if(!$this->_auth->hasIdentity()) {
        	$this->_redirect($this->view->adminURL('authentication', 'login'));
        }
        $this->view->user = $this->_auth->getIdentity();
		$this->_model =  new Admin_Model_DbTable_Post();
		$this->_category_model = new Application_Model_Category();
		$this->_user_model = new Admin_Model_DbTable_User();
    }

    public function indexAction()
    {
        // action body
    }

    public function listAction()
    {
		$this->view->q = $this->_request->getParam('q');
		$this->view->c = $this->_request->getParam('cate');
			
		$select = $this->_model->select()
		->order('id desc');
		
		if ($this->view->q)
			$select = $select->where('title like ?', '%'.$this->view->q.'%');
		if ($this->view->c)
			$select = $select->where('category_id = ?', $this->view->c);
		
		$this->view->users = $this->_user_model->filter($this->_user_model->fetchAll()->toArray());
		$all_post = $this->_model->fetchAll($select)->toArray();
		$paginator = Zend_Paginator::factory($all_post);
		$paginator->setItemCountPerPage(10);        
        $paginator->setPageRange(10);
        $currentPage = $this->_request->getParam('page',1);
        $paginator->setCurrentPageNumber($currentPage);
		
		$this->view->total = count($all_post);
		$this->view->posts = $paginator;
		$this->view->categories = $this->_category_model->filter(
		$this->_category_model->fetchAllOrderDesc());
		
		$this->view->headTitle('Danh sách bài viết');
    }

    
	public function editAction() 
	{
		$request = $this->getRequest();
		$id = intval($request->getParam('id'));
		if (!$id) {
			throw new Zend_Controller_Action_Exception('This page does not exist', 404);
		}
		
		//Get list model 
		$cate = new Application_Model_Category();
		$cates = $cate->filter($cate->fetchAllOrderDesc());
		
		//Change post if form submited
		if($this->getRequest())
		if ($this->getRequest()->isPost()) {
			$post= new Application_Model_Post();
			$post->updatePost(array(
				'title' => $request->getPost('title'),
				'content' => $request->getPost('content'),
				'published' => $request->getPost('published'),
				'category_id' => $request->getPost('category')
			), $id);
			$this->view->info = "Updated.";
			
		}
		
		
		$form = new Admin_Form_EditPost();
		
		$post= new Application_Model_Post();
		$result_post =  $post->getById($id);

		$this->view->post = $result_post;
		$this->view->headTitle("Chỉnh sửa ". $result_post->title);
	
		//Change form default value
		$form->id->setValue($result_post->id);
		$form->title->setValue($result_post->title);
		$form->content->setValue($result_post->content);
		$form->category->setValue($result_post->category_id);
		$form->published->setValue($result_post->published);
		$form->Create->setLabel( 'Lưu thay đổi');
		
		// var_dump($form->category);
		$form->category->setMultiOptions( $cates );
		
		
		$this->view->form = $form;
		$this->view->headScript()->appendFile($this->view->baseUrl().'/includes/ckeditor/ckeditor.js');
	}

    public function deleteAction()
    {
		$id = $this->getRequest()->getParam('id');
        $this->_model->delete('id = '.$id);
		die();
    }

	public function addAction() {
		$cate = new Application_Model_Category();
		$cates = $cate->filter($cate->fetchAllOrderDesc());
		$cates[0] = 'None';
		$form = new Admin_Form_EditPost();
		$form->category->setMultiOptions( $cates );
		
		$this->view->form = $form;
		$request = $this->getRequest();
		if($this->getRequest())
		if ($this->getRequest()->isPost()) {
			$post= new Admin_Model_Post();
			$post->addPost(array(
				'title' => $request->getPost('title'),
				'content' => $request->getPost('content'),
				'published' => $request->getPost('published'),
				'created' => time(),
				'author' =>$this->view->user->id,
				'category_id' => $request->getPost('category')
			));
		}
		
		$this->view->headScript()->appendFile($this->view->baseUrl().'/includes/ckeditor/ckeditor.js');
	}
	
	public function mergeQueryString(){
		if ($this->getRequest()->isPost()){
			return ;
		//  throw new Exception("mergeQueryString only works on GET requests.");
		  
		}    
		$q = $this->getRequest()->getQuery();
		$p = $this->getRequest()->getParams();
		if (empty($q)) {
		  //there's nothing to do.
		  return;
		}
		$action = $p['action'];
		$controller = $p['controller'];
		$module = $p['module'];
		unset($p['action'],$p['controller'],$p['module']);
		$params = array_merge($p,$q);
		$this->_helper->getHelper('Redirector')
				->setCode(301)            
				->gotoSimple(
						$action,
						$controller,
						$module,
						$params);

  }
}




